package com.light.gamecomponents.physics;

import java.util.ArrayList;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
import com.light.roller.Roller;
		
public class PhysicsChain {
	static final int CHAINPART = 40;
	
	protected PhysicsChainPart[] part = new PhysicsChainPart[CHAINPART];
	protected FixChainPart fixBeginPart = null;
	protected FixChainPart fixEndPart = null;
	protected Roller m_game;
	
	public PhysicsChain(Roller game, String resourceName, Vector2 initialPosition, Vector2 size) 
	{
		m_game = game;
		Vector2 partPosition = new Vector2(initialPosition);
		for(int i=0;i<CHAINPART;i++)
		{
			partPosition.x+=4;
			//partPosition.y+=1;
			part[i] = new PhysicsChainPart(game, resourceName,partPosition, size);
		}
	}
	public PhysicsChain(Roller game, String resourceName, Vector2 initialPosition, Vector2 size,int fixed) 
	{
		Vector2 partPosition = new Vector2(initialPosition);
		part[0] = new PhysicsChainPart(game, resourceName, partPosition,size);
		FixtureDef tempFixtureDef = PhysicsFactory.createFixtureDef(20, 0.0f, 0.0f);
		tempFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_NORMAL;
		tempFixtureDef.filter.maskBits = Roller.MASKBIT_NORMAL;
		Vector2 fixPoint = new Vector2(initialPosition);
		fixPoint.x += 1;
		fixBeginPart = new FixChainPart(new Rectangle(fixPoint.x,fixPoint.y,2,2),resourceName,tempFixtureDef);
		
		//partPosition.x+=1;
		
		for(int i=1;i<CHAINPART;i++)
		{
			partPosition.x+=4;
			//partPosition.y+=1;
			part[i] = new PhysicsChainPart(game, resourceName, partPosition, size);
		}
	}

	public void addToList(ArrayList<PhysicsComponents> physicsList)
	{
		for(int i=0;i<CHAINPART;i++)
		{
			physicsList.add(part[i]);
		}
	}
	
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {

		
		fixBeginPart.onLoadScene(scene, physicsWorld);
		if(fixBeginPart != null){
			//fixBeginPart.onLoadScene(scene, physicsWorld);
		}
		if(fixEndPart != null){
			fixEndPart.onLoadScene(scene, physicsWorld);
		}

		RevoluteJointDef jd = new RevoluteJointDef();
		jd.collideConnected = false;
		jd.initialize(part[0].getBody(),fixBeginPart.getBody(), fixBeginPart.getBody().getPosition());
		physicsWorld.createJoint(jd);
		
		for (int i = 0; i < CHAINPART-2; i++)
		{

			jd.initialize(part[i].getBody(),part[i+1].getBody(), part[i].getBody().getWorldCenter());
			physicsWorld.createJoint(jd);
		}
		
		//joint.setLimits(1, 1);
		
		
		
		//Roller.debugger.printText2.setText("body0:"+ part[0].getBody().getPosition().x+"  " +  part[0].getBody().getPosition().y
											//+"body1:"+ part[1].getBody().getPosition().x+"  " +  part[1].getBody().getPosition().y);
	}
	

	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		if(fixBeginPart != null){
			fixBeginPart.removeFromScene(scene, physicsWorld);
			fixBeginPart = null;
		}
		if(fixEndPart != null){
			fixEndPart.removeFromScene(scene, physicsWorld);
			fixEndPart = null;
		}
	}


	public void setPosition(int x, int y) {
			// TODO Auto-generated method stub
				
	}


}
